﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;
using System.Text;
using System.Web.Script.Serialization;
using System.Linq;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Small helper class to make creating Stores from Array data easier.
    /// </summary>    
    [Description("Small helper class to make creating Stores from Array data easier.")]
    public class ExtSimpleStore : Store
    {
        private List<ScriptReference> _references = new List<ScriptReference>();
        private List<ExtRecord> _fields = new List<ExtRecord>();
        private List<object> _data = new List<object>();         

        private List<SortInfo> _sortInfo = new List<SortInfo>();

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.data.Store", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.data.SimpleStore";
           

            //add reader
            if (fields.Count != 0)
            {
                // build items list
                StringBuilder tabList = new StringBuilder("[");

                for (int i = 0; i < fields.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        tabList.Append(",");
#if DEBUG
                        tabList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = fields[i].GetScriptDescriptors();

                    // write the item's config to the list
                    tabList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.ConfigOnly));
                }

                tabList.Append("]");

                desc.AddPropertyBase("fields", tabList);
            }

            //add selection model
            if (!string.IsNullOrEmpty(data.Description.ToString()))
            {
                //desc.AddPropertyBase("data", String.Format("[{0}]", data.Description.ToString().Trim().Replace(Environment.NewLine,"")));
                desc.AddPropertyBase("data", data.Description.ToString().Trim().Replace(Environment.NewLine, ""));
            }
          
            return list;
        }
       
        //public virtual void AddProperty(PropertyInfo property)
        //{
        //    if (property == null)
        //        throw new InvalidOperationException(String.Format("Property not found, 'ExtJsComponent.{0}'", property));

        //    Object value = property.GetValue(obj, null);

        //    if (j != 0)
        //        tabList.Append(",");

        //    if (value == null)
        //        tabList.Append("''");

        //    tabList.AppendFormat("'{0}'", value);
            
        //    j++;
        //}

        #region Public Properties        

        public String id { get; set; }

        /// <summary>
        /// An array of field definition objects, or field name strings.
        /// </summary>
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Description("An array of field definition objects, or field name strings.")]
        public List<ExtRecord> fields
        {
            get
            {
                return _fields;
            }
        }

        /// <summary>
        /// An array of field definition objects, or field name strings.
        /// </summary>                      
        [Description("An array of field definition objects, or field name strings.")]
        [Bindable(true)]        
        [DefaultValue("")]
        [Localizable(true)] 
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtData data { get; set; }

        ///// <summary>
        ///// An array of field definition objects, or field name strings.
        ///// </summary>
        //[NotifyParentProperty(true)]
        //[PersistenceMode(PersistenceMode.InnerProperty)]
        //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        //[Description("An array of field definition objects, or field name strings.")]
        //public List<object> data
        //{
        //    get
        //    {
        //        return _data;
        //    }
        //}



        #endregion
    }
}
